package com.sugon.controller.module;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.ModuleMaintainPlanItemEntity;
import com.sugon.modules.mold.service.IMoldMaintenanceManager;
import com.sugon.service.ModuleMaintainPlanItemService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 模具维护计划项关联Controller
 *
 * @author sugon
 * @date 2019-08-09 08:58:40
 */
@Controller
@RequestMapping("modulemaintainrelate")
public class ModuleMaintainPlanItemController {

    @Autowired
    private ModuleMaintainPlanItemService moduleMaintainPlanItemService;
    @Autowired
    private IMoldMaintenanceManager moduleMaintenanceManager;

    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ModuleMaintainPlanItemEntity> moduleMaintainRelateList = moduleMaintainPlanItemService.queryListByPlan(query);
        int total = moduleMaintainPlanItemService.queryTotalByPlan(query);
        PageUtils pageUtil = new PageUtils(moduleMaintainRelateList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    @AutoLog("模具维护规程关联--删除")
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return R.error("请选择一条记录！");
        }
        int rows = moduleMaintainPlanItemService.deleteBatch(ids);
        return R.ok("成功删除" + rows + "条关联");
    }

    @AutoLog("模具维护计划关联--增加")
    @RequestMapping("/savePlan")
    @ResponseBody
    public R savePlan(@RequestParam Map<String, Object> params) {
        String addResult = moduleMaintenanceManager.addModuleMaintenancePlanItem(params);
        return StrUtil.isEmpty(addResult) ? R.ok("添加成功!") : R.error(addResult);
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<ModuleMaintainPlanItemEntity> list = moduleMaintainPlanItemService.queryListByPlan(params);
        return R.ok().put("list", list);
    }

}
